<?php
/**
 * Example for class DirectoryReader
 */
require_once "DirectoryReader.php";
/**
* example
*
* Example class for DirectoryReader
*
* @copyright Copyright (c) 2012 Maxim Bondarev
* @license http://www.opensource.org/licenses/mit-license.php
* @author Maxim Bondarev
* @version 1.0 
*/
class ExampleDir extends DirectoryReader
{
    /**
     * @throws Exception
     */
    public function showDirectory()
    {
        if ( NULL === $this->files ) {      
            throw new Exception( 'Can\'t show dir ' . $this->dir );
        }else {
            
            foreach ( $this->files as $key=>$file ) {
                
                if ( NULL !== $file['permissions'] ) {
                    echo $file['permissions'] . ' ';
                }
                    
                if ( NULL !== $file['owner'] ) {
                    echo $file['owner']['owner'];
                    echo " ".$file['owner']['group'] ." ";
                }
                
                if ( $file['type'] === self::DR_FILE ) {                    
                        
                    echo $file['size'].' '.
                         date( "d M Y", $file['mtime']).' '.
                         $file['name'];
                }
                    
                if ( $file['type'] === self::DR_DIRECTORY ) {
                    echo '<a href="?d='. 
                    urlencode( $this->getCurrentDirectory() . "/" . $file['name'] ) . '" >' .
                    "<strong>{$file['name']}</strong></a>";
                }
                
                echo '<br />' . PHP_EOL;
            }
        }
        
        
    }
    
}

if ( isset( $_GET['d'] ) ){
    $dir = realpath ( $_GET['d'] );
} else {
    $dir = '/';
}

$config = array(    
    'store_full_path' => FALSE,
    'fu'
);

echo 
'<html>' . PHP_EOL .
'<head>' . PHP_EOL .
'<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' . PHP_EOL .
'</head>' . PHP_EOL .
'<body>' . PHP_EOL .
"<h2>Current directory: $dir</h2>" . PHP_EOL;
    
try {
    $directory = new ExampleDir( $dir, $config );   
    if ( NULL !== $directory->getParentDirectory() ) {
        echo '<a href="?d='. 
        urlencode( $directory->getParentDirectory() . '/' . $file['name'] ) . '" >' .
        'parent dir</a> <br />' . PHP_EOL ;
    }
        try {   
            $directory->showDirectory();
            echo '<br /><strong>Files size: ' . $directory->getDirectorySize() . '</strong>' . PHP_EOL;
            echo '<br /><strong>Total items: ' . $directory->DirectoryItemsCount() . '</strong>' . PHP_EOL;
            
            echo '<br /><strong>Files: ' . 
            $directory->DirectoryItemsCount( $directory::DR_FILE ) . '</strong>' . PHP_EOL;
            
            echo '<br /><strong>Directories: ' . 
            $directory->DirectoryItemsCount( $directory::DR_DIRECTORY ) . '</strong>' . PHP_EOL;
            
        }catch(Exception $e) {
            echo $e->getMessage();
        }
}catch(Exception $e) {
    echo $e->getMessage();
}

echo 
'</body>' . PHP_EOL .
'</html>' . PHP_EOL;